//
//  MediaViewController.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/9/30.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "MediaViewController.h"
#import "VideoTableView.h"
#import "RadioTableView.h"
#import "NetEaseDataService.h"
#import "UIImageView+WebCache.h"

#define navBarCenterViewWidth (kWidth-160)

#define kCollectionViewCellId @"CollectionViewCellId"

@interface MediaViewController ()

@end

@implementation MediaViewController {
    UICollectionView* _collectionView;
    
    UIView* navWhiteView;       //位于navBar上的白色滑条，随着collectionView的滑动左右滑动
    UIButton* navBarBtn1;      //视频
    UIButton* navBarBtn2;      //电台
    
    VideoTableView* _videoTableView;    //推荐阅读中的表视图
    RadioTableView* _radioTableView;    //推荐阅读中的表视图
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _setNavBar];
    [self _loadData];
    [self _createCollectionView];
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - navBar
- (void)_setNavBar {
    UIView* navBarCenterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, navBarCenterViewWidth, 25)];
    navBarCenterView.backgroundColor = [UIColor clearColor];
    navBarCenterView.layer.cornerRadius = 12.5;
    navBarCenterView.layer.borderColor = [UIColor whiteColor].CGColor;
    navBarCenterView.layer.borderWidth = 1.0;
    self.navigationItem.titleView = navBarCenterView;
    
    //白色滑条
    navWhiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, navBarCenterViewWidth/2, 25)];
    navWhiteView.backgroundColor = [UIColor whiteColor];
    navWhiteView.userInteractionEnabled = YES;
    navWhiteView.layer.cornerRadius = 12.5;
    [navBarCenterView addSubview:navWhiteView];
    
    //推荐阅读
    navBarBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, navBarCenterViewWidth/2, 25)];
    [navBarBtn1 setTitle:@"视频" forState:UIControlStateNormal];
    [navBarBtn1 setTitleColor:kNetEaseRedColor forState:UIControlStateNormal];
    navBarBtn1.titleLabel.font = [UIFont systemFontOfSize:14];
    [navBarBtn1 addTarget:self action:@selector(navBarBtn1Action) forControlEvents:UIControlEventTouchUpInside];
    [navBarCenterView addSubview:navBarBtn1];
    
    //我的订阅
    navBarBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(navBarCenterViewWidth/2, 0, navBarCenterViewWidth/2, 25)];
    [navBarBtn2 setTitle:@"电台" forState:UIControlStateNormal];
    [navBarBtn2 setTitleColor:kNetEaseRedColor forState:UIControlStateNormal];
    navBarBtn2.titleLabel.font = [UIFont systemFontOfSize:14];
    [navBarBtn2 addTarget:self action:@selector(navBarBtn2Action) forControlEvents:UIControlEventTouchUpInside];
    [navBarCenterView addSubview:navBarBtn2];
}

- (void)navBarBtn1Action {
    if (_collectionView.contentOffset.x/kWidth) {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
}
- (void)navBarBtn2Action {
    if (!_collectionView.contentOffset.x/kWidth) {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
}
#pragma mark - 加载视频与电台的数据
- (void)_loadData {
    //视频 tableView及其数据
    _videoTableView = [[VideoTableView alloc] initWithFrame:CGRectMake(0, 25, kWidth, kHeight-navBarHeight-tabBarHeight)];
    [NetEaseDataService mediaVideoRequestFrom:0 size:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"视听页 视频 数据获取失败 %@", result);
            return;
        }
//        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        NSArray* videoListArray = result[@"videoList"];
        NSArray* videoSidListArray = result[@"videoSidList"];
        if (videoListArray.count) _videoTableView.videoModelArray = videoListArray;
        if (videoSidListArray.count) _videoTableView.videoSidListArray = videoSidListArray;
    }];
    
    
    //电台 tableView及其数据
    _radioTableView = [[RadioTableView alloc] initWithFrame:CGRectMake(0, 25, kWidth, kHeight-navBarHeight-tabBarHeight)];
    [NetEaseDataService mediaRadioRequestBlock:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"视听页 电台 数据获取失败 %@", result);
            return;
        }
        //        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        NSArray* array = result[@"cList"];
        if (array.count) {
            _radioTableView.radioDataArray = array;
        }
    }];
}

#pragma mark - collectionView
- (void)_createCollectionView {
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(kWidth, kHeight-tabBarHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//滑动方向水平
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight-tabBarHeight) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellId];
    [self.view addSubview:_collectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellId forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        [cell.contentView addSubview:_videoTableView];
    } else {
        [cell.contentView addSubview:_radioTableView];
    }
    
    return cell;
}

#pragma mark - scrollViewDelegate
//正在划时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offX = scrollView.contentOffset.x;
    
    if (offX > 0 && offX < kWidth) {
        [navBarBtn1 setTitleColor:[UIColor colorWithRed:254.0/255 green:91.0/255 blue:88.0/255 alpha:1] forState:UIControlStateNormal];
        [navBarBtn2 setTitleColor:[UIColor colorWithRed:254.0/255 green:91.0/255 blue:88.0/255 alpha:1] forState:UIControlStateNormal];
        navWhiteView.transform = CGAffineTransformMakeTranslation(navBarCenterViewWidth/2*offX/kWidth, 0);
    }
}
//滑动结束时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offX = scrollView.contentOffset.x;
    NSInteger index = offX/kWidth;
    if (index == 0) {
        [navBarBtn1 setTitleColor:kNetEaseRedColor forState:UIControlStateNormal];
        [navBarBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [navWhiteView setFrame:CGRectMake(0, 0, navBarCenterViewWidth/2, 25)];
    } else {
        [navBarBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [navBarBtn2 setTitleColor:kNetEaseRedColor forState:UIControlStateNormal];
        [navWhiteView setFrame:CGRectMake(navBarCenterViewWidth/2, 0, navBarCenterViewWidth/2, 25)];
    }
}


@end
